package com.example.tfliteonwatch;


import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import org.tensorflow.lite.Interpreter;


import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.util.HashMap;

import java.util.Map;

import java.nio.FloatBuffer;

import com.example.tfliteonwatch.databinding.ActivityMainBinding;

public class MainActivity extends Activity {

    private TextView mTextView;
    private ActivityMainBinding binding;
    private Button  walking;
    private Button  jogging;
    private Button  stairs;
    private Button  sitting;
    private Button  standing;
    private Button  typing;
    private Button  writing;
    private Button  Clapping;
    private Button multiAction;
    private Button train;
    Interpreter interpreter;
    public void addaction(Button b){
        para.action.add(b.getText().toString());
    }
    private MappedByteBuffer loadModelFile() throws IOException{

        AssetFileDescriptor assetFileDescriptor = this.getAssets().openFd("linear_model_0921.tflite");

        FileInputStream fileInputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());

        FileChannel fileChannel = fileInputStream.getChannel();

        long startOffset = assetFileDescriptor.getStartOffset();

        long  length = assetFileDescriptor.getLength();

        return  fileChannel.map(FileChannel.MapMode.READ_ONLY,startOffset,length);

    }
    float doTrain(float val[][]) {



        // Run the training.

        float[][] var = new float[1][1];

        var[0][0] = 3.5f;

        float[] var2 = new float[1];

        var2[0] = 6.0f;

        FloatBuffer testImages = FloatBuffer.wrap(var[0]);

        float[] loss1 = new float[1];

        FloatBuffer label2 = FloatBuffer.wrap(var2);

        FloatBuffer loss2 = FloatBuffer.wrap(loss1);

        Map<String, Object> inputs = new HashMap<>();

        inputs.put("x", testImages.rewind());

        inputs.put("y", label2.rewind());

        Map<String, Object> outputs = new HashMap<>();

        outputs.put("loss", loss2);

        interpreter.runSignature(inputs, outputs, "train");

        return loss1[0];

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        para.action.clear();
        para.setTimeIndex=0;
        para.timelist.clear();
        para.modellist.clear();
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Log.d("","jinlaile");

        train=(Button) findViewById(R.id.train);
        train.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String var ="3";


                    interpreter  = TFLiteLoader.newInstance(getApplicationContext()).get("linear_model_0921.tflite");

                float [][] var2 = new float[1][1];

                var2[0][0] = Float.parseFloat(var);
                float loss6 = doTrain(var2);

                Log.d("",loss6+" ");
            }
        });


        walking=(Button) findViewById(R.id.btn_walking);
        walking.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addaction(walking);
                Intent intent=new Intent(MainActivity.this, setTime.class);
                startActivity(intent);
            }
        });

        jogging=(Button) findViewById(R.id.jogging);
        jogging.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addaction(jogging);
                Intent intent=new Intent(MainActivity.this, setTime.class);
                startActivity(intent);
            }
        });

//        stairs=(Button) findViewById(R.id.stairs);
//        stairs.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                addaction(stairs);
//                Intent intent=new Intent(MainActivity.this, setTime.class);
//                startActivity(intent);
//            }
//        });

        sitting=(Button) findViewById(R.id.sitting);
        sitting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addaction(sitting);
                Intent intent=new Intent(MainActivity.this, setTime.class);
                startActivity(intent);
            }
        });

//        standing=(Button) findViewById(R.id.standing);
//        standing.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                addaction(standing);
//                Intent intent=new Intent(MainActivity.this, setTime.class);
//                startActivity(intent);
//            }
//        });

        typing=(Button) findViewById(R.id.typing);
        typing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addaction(typing);
                Intent intent=new Intent(MainActivity.this, setTime.class);
                startActivity(intent);
            }
        });

        writing=(Button) findViewById(R.id.writing);
        writing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addaction(writing);
                Intent intent=new Intent(MainActivity.this, setTime.class);
                startActivity(intent);
            }
        });

        Clapping=(Button) findViewById(R.id.Clapping);
        Clapping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addaction(Clapping);
                Intent intent=new Intent(MainActivity.this, setTime.class);
                startActivity(intent);
            }
        });
        multiAction=(Button)findViewById(R.id.multi);
        multiAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, multiChoose.class);
                startActivity(intent);
            }
        });



    }
}